package com.yannan.lock.web.controller;//package com.yannan.lock.web.controller;

import com.yannan.lock.core.constant.CoreConstant;
import com.yannan.lock.core.controller.BaseController;
import com.yannan.lock.core.vo.ResultResponse;
import com.yannan.lock.web.dto.ReturnValueRspDTO;
import com.yannan.lock.web.dto.UserRspDTO;
import com.yannan.lock.web.service.InstallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * LoginController
 * 安装人员登录
 *
 * @author wu
 * @create 2016-10-25 13:49
 */
@Controller
public class LoginController extends BaseController {

    @Autowired
    private InstallUserService installUserService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultResponse login(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String phoneNumber = request.getParameter("phoneNumber");
        if(StringUtils.isEmpty(phoneNumber)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("手机号(phoneNumber)不能为空！");
            return  res;
        }
        try {
            UserRspDTO userRspDTO = installUserService.getLoginUserByPhone(phoneNumber);
            if (null == userRspDTO){
                res.setMessage("登录失败！安装人员不存在。");
                res.setRetCode(CoreConstant.RET_CODE_FAILED);
            }else{
                res.setData(userRspDTO);
                res.setMessage("登录成功！");
                res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            }

        }catch (Exception e){
            log.error(e.getMessage(),e);
            res.setMessage("登录失败！");
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            return res;
        }


        return res;
    }

    /**
     * 根据手机号获取验证码
     * @param request
     * @return
     */
    @RequestMapping("getVerificationCode")
    @ResponseBody
    public ResultResponse getVerificationCode(HttpServletRequest request){
        ResultResponse res = new ResultResponse();
        String phoneNumber = request.getParameter("phoneNumber");
        if(StringUtils.isEmpty(phoneNumber)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("手机号(phoneNumber)不能为空！");
            return  res;
        }

        res.setMessage("获取成功！");
        res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
        res.setData(new ReturnValueRspDTO("123456"));

        return res;
    }

}